package com.itmatter.classifier.rmi.client;

import com.itmatter.classifier.rmi.RemoteClassifierInterface;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import org.apache.log4j.Logger;

/**
 * 
 *
 */
public class RemoteClassifierClient {

    private static final Logger logger = Logger.getLogger(RemoteClassifierClient.class.getName());
    private String rmiPath = null;
    private RemoteClassifierInterface classifier = null;

    public static void main(String[] args) {

        String path = "//localhost/RemoteClassifierImpl";
        RemoteClassifierClient app = new RemoteClassifierClient(path);
        String cat = app.getClassification(args[0]);
        System.out.println(cat);
    }

    public RemoteClassifierClient(String path) {

        this.rmiPath = path;
        // Create and install a security manager
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
            logger.debug("Security manager installed.");
        } else {
            logger.debug("Security manager already exists.");
        }



        try {
            classifier = (RemoteClassifierInterface) Naming.lookup(rmiPath);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    public String getClassification(String content) {

        String category = null;

        try {
            category = classifier.getClassification(content);
            logger.debug("Test Client =>: Token: " + content + " Result: " + category);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        return category;
    }

    public void train(String content, String category) {

        try {
            classifier.train(content, category);
            logger.debug("Test Client =>: Token: " + content + " Result: " + category);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void sampleTrain(RemoteClassifierInterface cl) throws RemoteException {
        cl.train("Nobody owns the water.", "good");
        cl.train("the quick rabbit jumps fences", "good");
        cl.train("buy pharmaceuticals now", "bad");
        cl.train("make quick money in the online casino", "bad");
        cl.train("the quick brown fox jumps", "good");
    }

    public RemoteClassifierInterface getClassifier() {
        return classifier;
    }

    public String getRmiPath() {
        return rmiPath;
    }

    public void setRmiPath(String rmiPath) {
        this.rmiPath = rmiPath;
    }
}
